//
//  HomeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/21.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "HomeViewController.h"
#import "AppDelegate.h"
@interface HomeViewController ()
{
    int index;
    StatisticsViewController *statVc;
    UIImageView *blueBg;
    UIImageView *redBg;
}
@end

@implementation HomeViewController
@synthesize splitViewController,rootVC;
- (void)viewDidLoad {
    [super viewDidLoad];
    index =0;
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 动画选项设定
    animation.duration = 2; // 动画持续时间
    animation.repeatCount = 99999;
    animation.autoreverses = YES; // 动画结束时执行逆动画
    
    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:0.85]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.2]; // 结束时的倍率
    
    
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 动画选项设定
    animation2.duration = 2; // 动画持续时间
    animation2.repeatCount = 99999;
    animation2.autoreverses = YES; // 动画结束时执行逆动画
    
    // 缩放倍数
    animation2.fromValue = [NSNumber numberWithFloat:1.2]; // 开始时的倍率
    animation2.toValue = [NSNumber numberWithFloat:0.85]; // 结束时的倍率
    
    // 添加动画
    [blueBg.layer addAnimation:animation forKey:@"scale-layer"];
    
    // 添加动画
    [redBg.layer addAnimation:animation2 forKey:@"scale-layer"];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // Create the view that gets shown when no accessories are connected
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
    
    _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
    _sessionController = [[WSSessionController sharedController]init];
    
    
    
    for (int i=0; i<_accessoryList.count; i++) {
        _selectedAccessory = [_accessoryList objectAtIndex:i];
        NSArray *protocolStrings = [_selectedAccessory protocolStrings];
        
        
        for(NSString *protocolString in protocolStrings) {
            // protocol string matching
            if ([protocolString isEqualToString:@"com.woosim.wspr240"]) {
                
                [_sessionController setupControllerForAccessory:_selectedAccessory withProtocolString:protocolString];
                
                [_sessionController closeSession];
                if ([_sessionController openSession]) {
                    // [self performSegueWithIdentifier:@"sampleSegue" sender:tableView];
                    NSLog(@"openSession success");
                    
                    _selectedAccessory = nil;
                    return;
                }else
                {
                    NSLog(@"openSession fail ");
                }
                
                
            }                              
        }
    }
    
    
   


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)initUI
{
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"index2_bg"]];
    [self.view setBackgroundColor:bgColor];
    
    
    [_dyBtn bk_whenTapped:^{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.opType = DianYuan;
        [self pop];
        [self presentViewController:splitViewController animated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];   
    
        
    }];
    [_dzBtn bk_whenTapped:^{
        index = 0;
        GesturePasswordController *gpc=[[GesturePasswordController alloc] init];
        gpc.modalPresentationStyle=UIModalPresentationFormSheet;
        gpc.delegate=self;
        [self presentViewController:gpc animated:YES completion:nil];
     
    }];

    
    blueBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    blueBg.center = _dyBtn.center;
    [blueBg setContentMode:UIViewContentModeScaleAspectFit];
    blueBg.image = [UIImage imageNamed:@"blue_q"];
    [self.view addSubview:blueBg];
    
    redBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    redBg.center = _dzBtn.center;
    [redBg setContentMode:UIViewContentModeScaleAspectFit];
    redBg.image = [UIImage imageNamed:@"red_q"];
    [self.view addSubview:redBg];
    
    

    
    
    
    [_linkView bk_whenTapped:^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com/"]]; 
    }];

    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appHasGoneInForeground)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
   
}



-(void)appHasGoneInForeground
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 动画选项设定
    animation.duration = 2; // 动画持续时间
    animation.repeatCount = 99999;
    animation.autoreverses = YES; // 动画结束时执行逆动画
    
    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:0.85]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.2]; // 结束时的倍率
    
    
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 动画选项设定
    animation2.duration = 2; // 动画持续时间
    animation2.repeatCount = 99999;
    animation2.autoreverses = YES; // 动画结束时执行逆动画
    
    // 缩放倍数
    animation2.fromValue = [NSNumber numberWithFloat:1.2]; // 开始时的倍率
    animation2.toValue = [NSNumber numberWithFloat:0.85]; // 结束时的倍率
    
    // 添加动画
    [blueBg.layer addAnimation:animation forKey:@"scale-layer"];
    
    // 添加动画
    [redBg.layer addAnimation:animation2 forKey:@"scale-layer"];

}

-(void)pop
{
    splitViewController = [[BaseSplitViewController alloc]init];
    rootVC = [[RootVC alloc]init];
    [splitViewController setValue:[NSNumber numberWithFloat:60.0] forKey:@"_masterColumnWidth"];
    //  [rootVC.view setFrame:CGRectMake(0, 0, 200, SCREEN_WIDTH)];
    [splitViewController setPresentsWithGesture:YES];
    splitViewController.view.backgroundColor = RGBCOLOR(88,34,8);
    
    //主页
    MainViewController *mainVc = [[MainViewController alloc] init];
    UINavigationController *mainnav = [[UINavigationController alloc] initWithRootViewController:mainVc];
    UITabBarController *tab=[[UITabBarController alloc] init];
    
    
    //设置页
    SettingViewController *settingVc = [[SettingViewController alloc] init];
    statVc =[[StatisticsViewController alloc] init];
    
    
    PrintViewController *printVc = [[PrintViewController alloc] init];
   
    
    BaseSplitViewController  *settingsplitViewController = [[BaseSplitViewController alloc]init];
    [settingsplitViewController setValue:[NSNumber numberWithFloat:400] forKey:@"_masterColumnWidth"];
    [settingsplitViewController setPresentsWithGesture:YES];
    [settingsplitViewController setViewControllers:[NSArray arrayWithObjects:settingVc,nil, nil]];//横向时index:0左边导航展示，index:1为右边显示的view
    settingsplitViewController.view.backgroundColor = RGBCOLOR(88,34,8);
    UIColor *sbgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"beijingtur"]];
    [settingsplitViewController.view setBackgroundColor:sbgColor];
    
    // [statVc.view setBackgroundColor:sbgColor];
    
//    BaseSplitViewController  *statViewController = [[BaseSplitViewController alloc]init];
//    [statViewController setValue:[NSNumber numberWithFloat:800] forKey:@"_masterColumnWidth"];
//    [statViewController setPresentsWithGesture:YES];
//    [statViewController setViewControllers:[NSArray arrayWithObjects:statVc,nil, nil]];//横向时index:0左边导航展示，index:1为右边显示的view
//    statViewController.view.backgroundColor = RGBCOLOR(88,34,8);
//    [statViewController.view setBackgroundColor:sbgColor];
    
    tab.viewControllers=@[mainnav,settingsplitViewController];
    tab.tabBar.hidden=YES;
    
     [splitViewController setViewControllers:[NSArray arrayWithObjects:rootVC,tab, nil]];//横向时index:0左边导航展示，index:1为右边显示的view
    
    
    [rootVC.settingBtn bk_whenTapped:^{
        tab.selectedIndex=1;
        
    }];
    
    [rootVC.homeBtn bk_whenTapped:^{
        [self.splitViewController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [rootVC.statBtn bk_whenTapped:^{
        index = 1;
         AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//        if(appDelegate.opType==DianYuan)
//        {
//            GesturePasswordController *gpc=[[GesturePasswordController alloc] init];
//            gpc.modalPresentationStyle=UIModalPresentationFormSheet;
//            gpc.delegate=self;
//            [self.splitViewController presentViewController:gpc animated:YES completion:nil];
//        }else
//        {
//            [self.splitViewController presentViewController:statVc animated:YES completion:nil];
//        }
        GesturePasswordController *gpc=[[GesturePasswordController alloc] init];
        gpc.modalPresentationStyle=UIModalPresentationFormSheet;
        gpc.delegate=self;
        [self.splitViewController presentViewController:gpc animated:YES completion:nil];
        
    }];
    
    
    [rootVC.printBtn bk_whenTapped:^{
         [self.splitViewController presentViewController:printVc animated:YES completion:nil];
    }];
    
    
    
    
    [rootVC.addPayBtn bk_whenTapped:^{
        PayListViewController *payListVC = [[PayListViewController alloc] init];
        payListVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        payListVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        self.definesPresentationContext = YES;
        [self.splitViewController presentViewController:payListVC animated:YES completion:nil];
    }];
    
    
    
    
    
}


-(void)gesturePasswordSuccess
{
  if(index==0)
  {
      [self pop];
      [self presentViewController:splitViewController animated:YES completion:nil];
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      appDelegate.opType = DianZhang;
  }else if (index==1)
  {
      [self.splitViewController presentViewController:statVc animated:YES completion:nil];
  }
   
}



#pragma mark--蓝牙打印机
- (void)_accessoryDidConnect:(NSNotification *)notification
{
    
    // add new accessory to accessory list
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
    [_accessoryList addObject:connectedAccessory];
    
    // update indexPath for added accessory
    //NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([_accessoryList count] - 1) inSection:0];
  //  [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
    
    
 
    
}

- (void)_accessoryDidDisconnect:(NSNotification *)notification
{
    
    EAAccessory *disconnectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
    
    // find index of disconnected accessory in accessory list
    int disconnectedAccessoryIndex = 0;
    for(EAAccessory *accessory in _accessoryList) {
        if ([disconnectedAccessory connectionID] == [accessory connectionID]) {
            break;
        }
        disconnectedAccessoryIndex++;
    }
    
    if (disconnectedAccessoryIndex < [_accessoryList count]) {
        // remove disconnected accessory from list
        [_accessoryList removeObjectAtIndex:disconnectedAccessoryIndex];
        
      //  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:disconnectedAccessoryIndex inSection:0];
     //   [[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } else {
        NSLog(@"could not find disconnected accessory in accessory list");
    }
}

@end
